﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.Model;

namespace Test.Utils
{
    public class CommonUtil
    {
        /// <summary>
        /// Format json string
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ConvertJsonString(string str)
        {
            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(str);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            if (obj != null)
            {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                {
                    Formatting = Formatting.Indented,
                    Indentation = 4,
                    IndentChar = ' '
                };
                serializer.Serialize(jsonWriter, obj);
                return textWriter.ToString();
            }
            else
            {
                return str;
            }
        }

        public static DeviceParameterModel getDeviceParameterModel(string parameterID) {
            DeviceParameterModel model = new DeviceParameterModel();
            switch (parameterID)
            {
                case "0x0001":
                case "0xF01D":
                    model.parameterLength = "4";
                    model.parameterUnit = "second";
                    model.deviceType = "GAV";
                    break;
                case "0x0010":
                case "0x0011":
                case "0x0012":
                case "0x0013":
                case "0x0017":
                case "0xF000":
                case "0xF004":
                case "0xF030":
                    model.parameterLength = "/";
                    model.parameterUnit = "/";
                    model.deviceType = "GAV";
                    break;
                case "0x0018":
                case "0xF005":
                case "0xF018":
                case "0xF02B":
                    model.parameterLength = "4";
                    model.parameterUnit = "/";
                    model.deviceType = "GAV";
                    break;
                case "0x0027":
                case "0x0029":
                case "0x0056":
                    model.parameterLength = "4";
                    model.parameterUnit = "second";
                    model.deviceType = "V";
                    break;
                case "0x0030":
                    model.parameterLength = "4";
                    model.parameterUnit = "degree";
                    model.deviceType = "V";
                    break;
                case "0x0055":
                    model.parameterLength = "4";
                    model.parameterUnit = "km/h";
                    model.deviceType = "V";
                    break;
                case "0x0080":
                    model.parameterLength = "4";
                    model.parameterUnit = "0.1km";
                    model.deviceType = "V";
                    break;
                case "0xF001":
                case "0xF002":
                case "0xF003":
                    model.parameterLength = "2";
                    model.parameterUnit = "millivolt";
                    model.deviceType = "V";
                    break;
                case "0xF006":
                case "0xF007":
                case "0xF009":
                case "0xF00A":
                case "0xF00B":
                case "0xF00C":
                case "0xF00D":
                case "0xF00F":
                case "0xF010":
                case "0xF011":
                case "0xF014":
                case "0xF017":
                case "0xF01A":
                case "0xF01B":
                case "0xF01F":
                case "0xF02C":
                case "0xF02D":
                    model.parameterLength = "1";
                    model.parameterUnit = "/";
                    model.deviceType = "GAV";
                    break;
                case "0xF00E":
                case "0xF013":
                    model.parameterLength = "2";
                    model.parameterUnit = "second";
                    model.deviceType = "GA";
                    break;
                case "0xF012":
                    model.parameterLength = "1";
                    model.parameterUnit = "minute";
                    model.deviceType = "GAV";
                    break;
                case "0xF015":
                    model.parameterLength = "2";
                    model.parameterUnit = "/";
                    model.deviceType = "V";
                    break;
                case "0xF016":
                    model.parameterLength = "1";
                    model.parameterUnit = "/";
                    model.deviceType = "V";
                    break;
                case "0xF019":
                    model.parameterLength = "1";
                    model.parameterUnit = "/";
                    model.deviceType = "GAV";
                    break;
                case "0xF01C":
                case "0xF021":
                    model.parameterLength = "2";
                    model.parameterUnit = "/";
                    model.deviceType = "GA";
                    break;
                case "0xF01E":
                case "0xF022":
                case "0xF02E":
                case "0xF02F":
                    model.parameterLength = "4";
                    model.parameterUnit = "second";
                    model.deviceType = "GA";
                    break;
                case "0xF020":
                case "0xF02A":
                    model.parameterLength = "1";
                    model.parameterUnit = "/";
                    model.deviceType = "GA";
                    break;
                case "0xF023":
                case "0xF029":
                    model.parameterLength = "1";
                    model.parameterUnit = "/";
                    model.deviceType = "G";
                    break;
                case "0xF024":
                case "0xF025":
                case "0xF026":
                case "0xF027":
                    model.parameterLength = "2";
                    model.parameterUnit = "/";
                    model.deviceType = "G";
                    break;
                case "0xF028":
                    model.parameterLength = "4";
                    model.parameterUnit = "second";
                    model.deviceType = "V";
                    break;
                default:
                    return null;
            }
            return model;
        }

        public static string getCommandWordForDevice(int word)
        {
            string txt = string.Empty;
            switch (word) 
            {
                case 4:
                case 5:
                case 32:
                case 36:
                    txt = "This command word for AGV";
                    break;
                case 33:
                case 34:
                case 35:
                    txt = "This command word for V";
                    break;
                default:
                    txt = "This command word is undefined";
                    break;
            }
            return txt;
        }
    }
}
